{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": 3
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python_defaultSpec_1596366815527",
   "display_name": "Python 3.6.4 64-bit ('base': conda)"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from docx import Document\n",
    "\n",
    "document = Document()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "<docx.styles.styles.Styles at 0x17be565af60>"
     },
     "metadata": {},
     "execution_count": 13
    }
   ],
   "source": [
    "styles = document.styles\n",
    "styles"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "Normal\nHeader\nHeader Char\nFooter\nFooter Char\nHeading 1\nHeading 2\nHeading 3\nHeading 4\nHeading 5\nHeading 6\nHeading 7\nHeading 8\nHeading 9\nDefault Paragraph Font\nNormal Table\nNo List\nNo Spacing\nHeading 1 Char\nHeading 2 Char\nHeading 3 Char\nTitle\nTitle Char\nSubtitle\nSubtitle Char\nList Paragraph\nBody Text\nBody Text Char\nBody Text 2\nBody Text 2 Char\nBody Text 3\nBody Text 3 Char\nList\nList 2\nList 3\nList Bullet\nList Bullet 2\nList Bullet 3\nList Number\nList Number 2\nList Number 3\nList Continue\nList Continue 2\nList Continue 3\nmacro\nMacro Text Char\nQuote\nQuote Char\nHeading 4 Char\nHeading 5 Char\nHeading 6 Char\nHeading 7 Char\nHeading 8 Char\nHeading 9 Char\nCaption\nStrong\nEmphasis\nIntense Quote\nIntense Quote Char\nSubtle Emphasis\nIntense Emphasis\nSubtle Reference\nIntense Reference\nBook Title\nTOC Heading\nTable Grid\nLight Shading\nLight Shading Accent 1\nLight Shading Accent 2\nLight Shading Accent 3\nLight Shading Accent 4\nLight Shading Accent 5\nLight Shading Accent 6\nLight List\nLight List Accent 1\nLight List Accent 2\nLight List Accent 3\nLight List Accent 4\nLight List Accent 5\nLight List Accent 6\nLight Grid\nLight Grid Accent 1\nLight Grid Accent 2\nLight Grid Accent 3\nLight Grid Accent 4\nLight Grid Accent 5\nLight Grid Accent 6\nMedium Shading 1\nMedium Shading 1 Accent 1\nMedium Shading 1 Accent 2\nMedium Shading 1 Accent 3\nMedium Shading 1 Accent 4\nMedium Shading 1 Accent 5\nMedium Shading 1 Accent 6\nMedium Shading 2\nMedium Shading 2 Accent 1\nMedium Shading 2 Accent 2\nMedium Shading 2 Accent 3\nMedium Shading 2 Accent 4\nMedium Shading 2 Accent 5\nMedium Shading 2 Accent 6\nMedium List 1\nMedium List 1 Accent 1\nMedium List 1 Accent 2\nMedium List 1 Accent 3\nMedium List 1 Accent 4\nMedium List 1 Accent 5\nMedium List 1 Accent 6\nMedium List 2\nMedium List 2 Accent 1\nMedium List 2 Accent 2\nMedium List 2 Accent 3\nMedium List 2 Accent 4\nMedium List 2 Accent 5\nMedium List 2 Accent 6\nMedium Grid 1\nMedium Grid 1 Accent 1\nMedium Grid 1 Accent 2\nMedium Grid 1 Accent 3\nMedium Grid 1 Accent 4\nMedium Grid 1 Accent 5\nMedium Grid 1 Accent 6\nMedium Grid 2\nMedium Grid 2 Accent 1\nMedium Grid 2 Accent 2\nMedium Grid 2 Accent 3\nMedium Grid 2 Accent 4\nMedium Grid 2 Accent 5\nMedium Grid 2 Accent 6\nMedium Grid 3\nMedium Grid 3 Accent 1\nMedium Grid 3 Accent 2\nMedium Grid 3 Accent 3\nMedium Grid 3 Accent 4\nMedium Grid 3 Accent 5\nMedium Grid 3 Accent 6\nDark List\nDark List Accent 1\nDark List Accent 2\nDark List Accent 3\nDark List Accent 4\nDark List Accent 5\nDark List Accent 6\nColorful Shading\nColorful Shading Accent 1\nColorful Shading Accent 2\nColorful Shading Accent 3\nColorful Shading Accent 4\nColorful Shading Accent 5\nColorful Shading Accent 6\nColorful List\nColorful List Accent 1\nColorful List Accent 2\nColorful List Accent 3\nColorful List Accent 4\nColorful List Accent 5\nColorful List Accent 6\nColorful Grid\nColorful Grid Accent 1\nColorful Grid Accent 2\nColorful Grid Accent 3\nColorful Grid Accent 4\nColorful Grid Accent 5\nColorful Grid Accent 6\n"
    }
   ],
   "source": [
    "for style in styles:\n",
    "    print(style.name)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "<docx.text.paragraph.Paragraph at 0x17be2d864e0>"
     },
     "metadata": {},
     "execution_count": 3
    }
   ],
   "source": [
    "\n",
    "paragraph = document.add_paragraph(\"Lorem ipsum dolor sit amet.\")\n",
    "prior_paragraph = paragraph.insert_paragraph_before(\"Lorem ipsum\")\n",
    "document.add_heading(\"The REAL meaning of the universe\")\n",
    "document.add_heading(\"The role of dolphins\", level=2)\n",
    "document.add_page_break()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "\nparrot, possibly dead\nFoo bar to you.\nAnd a hearty foo bar to you too sir!\n"
    }
   ],
   "source": [
    "table = document.add_table(rows=2, cols=2)\n",
    "cell = table.cell(0, 1)\n",
    "cell.text = \"parrot, possibly dead\"\n",
    "row = table.rows[1]\n",
    "row.cells[0].text = \"Foo bar to you.\"\n",
    "row.cells[1].text = \"And a hearty foo bar to you too sir!\"\n",
    "\n",
    "for row in table.rows:\n",
    "    for cell in row.cells:\n",
    "        print(cell.text)\n",
    "row = table.add_row()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# get table data -------------\n",
    "items = (\n",
    "    (7, \"1024\", \"Plush kittens\"),\n",
    "    (3, \"2042\", \"Furbees\"),\n",
    "    (1, \"1288\", \"French Poodle Collars, Deluxe\"),\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "(3, '2042', 'Furbees')"
     },
     "metadata": {},
     "execution_count": 11
    }
   ],
   "source": [
    "items[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# add table ------------------\n",
    "table = document.add_table(1, 3)\n",
    "\n",
    "# populate header row --------\n",
    "heading_cells = table.rows[0].cells\n",
    "heading_cells[0].text = \"Qty\"\n",
    "heading_cells[1].text = \"SKU\"\n",
    "heading_cells[2].text = \"Description\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "output_type": "error",
     "ename": "AttributeError",
     "evalue": "'tuple' object has no attribute 'qty'",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-10-70333056d336>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mitem\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mitems\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m     \u001b[0mcells\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtable\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0madd_row\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcells\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 4\u001b[1;33m     \u001b[0mcells\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtext\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mstr\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mitem\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mqty\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      5\u001b[0m     \u001b[0mcells\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtext\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mitem\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msku\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      6\u001b[0m     \u001b[0mcells\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtext\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mitem\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdesc\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mAttributeError\u001b[0m: 'tuple' object has no attribute 'qty'"
     ]
    }
   ],
   "source": [
    "\n",
    "# add a data row for each item\n",
    "for item in items:\n",
    "    cells = table.add_row().cells\n",
    "    cells[0].text = str(item.qty)\n",
    "    cells[1].text = item.sku\n",
    "    cells[2].text = item.desc\n",
    "    \n",
    "table.style = \"LightShading-Accent1\"\n",
    "\n",
    "row_count = len(table.rows)\n",
    "col_count = len(table.columns)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "document.save(\"test.docx\")"
   ]
  }
 ]
}